package com.bitrate.dagger.server.im.config;

import java.util.Collections;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

import lombok.extern.slf4j.Slf4j;

/**
 * mysql配置类
 */
@Slf4j
@Configuration
@MapperScan(basePackages = {
		"com.bitrate.dagger.server.im.mapper.mysql" }, sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MySQLServerConfig {

	@Autowired
	private PaginationInnerInterceptor paginationInnerInterceptor;

	private static final String MAPPER_LOCATION = "classpath*:mapper/mysql/*.xml";

	@Bean(name = "mysqlDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.mysql-service")
	@Primary
	public DataSource mysqlDataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean(name = "mysqlSqlSessionFactory")
	@Primary
	public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)
			throws Exception {
		log.debug("--------------MysqlSqlSessionFactory Init----------------");
		// 注意这里一定要用MybatisSqlSessionFactoryBean，如果用SqlSessionFactory，配置无效
		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));

		// 不在这里注入分页插件，会失效，（MyBatisPlusConfig只负责注册分页插件）
		MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
		paginationInnerInterceptor.setMaxLimit(-1L);
		paginationInnerInterceptor.setDbType(DbType.MYSQL);
		// 开启 count 的 join 优化,只针对部分 left join
		paginationInnerInterceptor.setOptimizeJoin(true);
		mybatisPlusInterceptor.setInterceptors(Collections.singletonList(paginationInnerInterceptor));
		bean.setPlugins(mybatisPlusInterceptor);
		return bean.getObject();
	}

	@Bean(name = "mysqlTransactionManager")
	@Primary
	public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "mysqlSqlSessionTemplate")
	@Primary
	public SqlSessionTemplate mysqlSqlSessionTemplate(
			@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
